package hotel.action;

import hotel.model.IUser;
import hotel.service.ServiceManager;
import hotel.service.UserService;
import hotel.action.ForwardActionTo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
 * This class get list of users from DataBase 
 * @author Alex
 */
public class UserListAction extends Action
{
	private static final Logger logger = Logger.getLogger(UserListAction.class);
	private static final String USERLISTACTION_ERROR = "Error in UserListAction:";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception 
    {
		String mapper = null;
		try
		{
			UserService userService = ServiceManager.getDefault().getUserService();
	        List<IUser> users = null;
	        users = userService.getUsers();
	        request.setAttribute("users", users);
	        mapper = ForwardActionTo.SUCCESS;
		}
		catch(NullPointerException e)
		{
			logger.error(USERLISTACTION_ERROR + e);
			mapper = ForwardActionTo.ERROR;
		}
		
        return mapping.findForward(mapper);
    }
}